package jp.sourceforge.akjrcp.ffmpeg;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import jp.sourceforge.akjrcp.util.image.ImageUtils;

public class JpegPipeWriter {
	OutputStream outputStream;
	private float quality=1.0f;
	public JpegPipeWriter(OutputStream output){
		this.outputStream=output;
	}
	public void writeJpeg(BufferedImage image) throws IOException{
			//log.infojpeg");
			
			ByteArrayOutputStream bout=new ByteArrayOutputStream();
			ImageUtils.writeJpegWithQuality(image,bout,quality);// i don't know what they done inside.
			bout.close();
			
			byte[] jpegbyte=bout.toByteArray();
			//for (int i = 0; i < jpegbyte.length; i++) {
			//log.infojpegbyte:"+jpegbyte.length);
				outputStream.write(jpegbyte);//is ok?
				//outputStream.flush();
			//	log.infowritten");
				//outputStream.flush(); //close?
			//}
		
	}
	
	public void close() throws IOException{
		if(outputStream!=null){
			outputStream.flush();
			outputStream.close();
		}
	}
	
	//copy direct;
	public void writeJpeg(InputStream input) throws IOException{
		int b;
		
			while((b=input.read())!=-1){
				outputStream.write(b);
			}
			input.close();
		
	}
}
